package com.qjl.shopping.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qjl.shopping.bean.Admin;
import com.qjl.shopping.controller.vm.AdminVM;
import com.qjl.shopping.dao.AdminMapper;
import com.qjl.shopping.exception.ServiceException;
import com.qjl.shopping.service.AdminService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * @Author liguanghui
 * @Date 2023/5/1
 */
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService {
    @Autowired
    private AdminMapper adminMapper;

    @Override
    public Page<Admin> selectPage(int pageNum, int pageSize) {
        Page<Admin> page = new Page<>(pageNum,pageSize);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.orderByDesc("admin_id");
        Page<Admin> adminPage = adminMapper.selectPage(page,queryWrapper);
        return adminPage;
    }

    @Override
    public AdminVM login(String ac, String pwd) {
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("admin_ac",ac).eq("admin_pwd",pwd);
        List<Admin> admins = adminMapper.selectList(queryWrapper);
        if(admins.size()!=1){
            throw new ServiceException(400,"请确认用户名和密码");
        }
        AdminVM adminVM = new AdminVM();
        BeanUtils.copyProperties(admins.get(0),adminVM);
        return adminVM;
    }
}
